package cn.com.chengmandian.bike.service.impl;

import cn.com.chengmandian.bike.model.EbDeviceBike;
import cn.com.chengmandian.bike.model.EbPriceRule;
import cn.com.chengmandian.bike.model.EbRentOrder;
import cn.com.chengmandian.bike.model.EbRentPenalty;
import cn.com.chengmandian.bike.mapper.EbRentPenaltyMapper;
import cn.com.chengmandian.bike.service.IEbPriceRuleService;
import cn.com.chengmandian.bike.service.IEbRentPenaltyService;
import cn.com.chengmandian.core.common.constants.CommonConstant;
import cn.com.chengmandian.core.common.utils.BillNoUtil;
import cn.com.chengmandian.service.bike.constants.RentPenaltyStatusEnum;
import cn.com.chengmandian.service.bike.constants.RentStopTypeEnum;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * <p>
 * 电单车运营-分时租赁-罚金 服务实现类
 * </p>
 *
 * @author AutoGenerator
 * @since 2023-07-10
 */
@Service
public class EbRentPenaltyServiceImpl extends ServiceImpl<EbRentPenaltyMapper, EbRentPenalty> implements IEbRentPenaltyService {

    @Autowired
    private IEbPriceRuleService priceRuleService;

    @Override
    public EbRentPenalty getByOrderId(Integer orderId) {
        return this.getOne(new LambdaQueryWrapper<EbRentPenalty>().eq(EbRentPenalty::getOrderId, orderId).last("limit 0,1"));
    }

    @Override
    public EbRentPenalty getByMember(Integer memberId) {
        return this.getOne(new LambdaQueryWrapper<EbRentPenalty>().eq(EbRentPenalty::getMemberId,memberId).eq(EbRentPenalty::getStatus, RentPenaltyStatusEnum.WAIT_PAY.getCode()).last("limit 0,1"));
    }

    @Override
    public void addPayRecord(Integer id, Integer payId, String payNo) {
        this.update(new LambdaUpdateWrapper<EbRentPenalty>()
                .set(EbRentPenalty::getPayId, payId)
                .set(EbRentPenalty::getPayNo, payNo)
                .eq(EbRentPenalty::getId, id));
    }

    @Override
    public EbRentPenalty getByPayId(Integer payId) {
        return this.getOne(new LambdaQueryWrapper<EbRentPenalty>().eq(EbRentPenalty::getPayId, payId).last("limit 0,1"));
    }

    @Override
    public void rentPenalty(EbRentOrder rentOrder, EbDeviceBike deviceBike, Boolean isStationDispatch) {
        if(this.getOne(new LambdaQueryWrapper<EbRentPenalty>()
                .eq(EbRentPenalty::getOrderId, rentOrder.getId())
                .last("limit 0,1")) != null) return;

        // 运营配置收费规则
        EbPriceRule priceRule = priceRuleService.getRule(deviceBike.getAreaId());
        // 违约金
        BigDecimal penaltyMoney = BigDecimal.ZERO;
        RentStopTypeEnum stopTypeEnum = null;
        // 服务区检测
        if (deviceBike.getIsInArea().equals(CommonConstant.ZERO)) {
            // 系统允许超区
            if (priceRule.getIsOpenDispatch().equals(CommonConstant.ONE)) {
                // 有调度费
                if (priceRule.getSuperDispatchFee().compareTo(new BigDecimal(0)) > 0) {
                    penaltyMoney = priceRule.getSuperDispatchFee();
                    stopTypeEnum = RentStopTypeEnum.AREA_OUTER;
                }
            }
        }else if(deviceBike.getStationId() == null && isStationDispatch){
            // 系统允许站外
            if (priceRule.getIsOpenDispatch().equals(CommonConstant.ONE)) {
                // 有调度费
                if (priceRule.getStationDispatchFee().compareTo(new BigDecimal(0)) > 0) {
                    penaltyMoney = priceRule.getStationDispatchFee();
                    stopTypeEnum = RentStopTypeEnum.STATION_OUTER;
                }
            }
        }

        if(penaltyMoney.compareTo(BigDecimal.ZERO) <= 0) return;
        this.createRecord(deviceBike.getAreaId(), rentOrder.getId(), rentOrder.getMemberId(), stopTypeEnum, penaltyMoney, penaltyMoney);
    }

    @Override
    public void createRecord(Integer areaId, Integer orderId, Integer memberId, RentStopTypeEnum stopTypeEnum,BigDecimal stopMoney, BigDecimal money) {
        EbRentPenalty ebRentPenalty = new EbRentPenalty();
        ebRentPenalty.setPenaltyNo(BillNoUtil.getSimpleNo("RP-"));
        ebRentPenalty.setAreaId(areaId);
        ebRentPenalty.setOrderId(orderId);
        ebRentPenalty.setMemberId(memberId);
        ebRentPenalty.setStopType(stopTypeEnum.getCode());
        ebRentPenalty.setStopMoney(stopMoney);
        ebRentPenalty.setMoney(money);
        ebRentPenalty.setPayMoney(money);
        ebRentPenalty.setStatus(RentPenaltyStatusEnum.WAIT_PAY.getCode());
        this.save(ebRentPenalty);
    }
}
